package cn.ronghuanet.base.service.impl;

import cn.ronghuanet.base.mapper.BaseMapper;
import cn.ronghuanet.base.query.BaseQuery;
import cn.ronghuanet.base.service.IBaseService;
import cn.ronghuanet.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation= Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional //增删改都需要事务
    public void insertOne(T t) {
        baseMapper.insertOne(t);
    }

    @Override
    @Transactional //增删改都需要事务
    public void updateOne(T t) {
        baseMapper.updateOne(t);
    }

    @Override
    @Transactional //增删改都需要事务
    public void deleteOne(Long id) {
       baseMapper.deleteOne(id);
    }

    @Override
    @Transactional //增删改都需要事务
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }

    @Override
    public T getOne(Long id) {
        return baseMapper.getOne(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    //分页查询
    @Override
    public PageList<T> queryPage(BaseQuery tQuery) {
        //1.查询总条数
        Integer total = baseMapper.queryCount(tQuery);
        //2.当前页的数据
        if (total==null || total==0){
            return  new PageList<>();//返回一个无结果新对象
        }
        List<T> list = baseMapper.queryData(tQuery);
        //全参构造
        return new PageList<>(total,list);//返回一个有结果新对象
    }
}
